Imports System
Imports System.Collections
Imports System.Text
Imports System.XML
Imports DotNetNuke.Common.Utilities

Namespace DotNetNuke.Modules.ProjectTracker

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    '''		The HistoryController class provides business layer methods
    '''		for History objects used by the ProjectTracker module.
    ''' </summary>
    ''' <remarks></remarks>
    ''' <history>
    '''		[MyGeneration]	3/18/2006 5:08:40 PM	Code file automatically generated from DotNetNuke.PT_History by MyGeneration v1.1.5.1
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class HistoryController

        Implements Entities.Modules.IPortable


        Public Sub New()
            ' default object initialisation	
        End Sub

#Region " Public Methods "

        ''' <summary>
        ''' Adds a new History record
        ''' </summary>
        ''' <returns>Identifier for the newly created object</returns>
        Public Function AddHistory(ByVal historyInfo As HistoryInfo) As Integer
            Return CType(DataProvider.Instance().AddHistory(historyInfo.ModuleID, historyInfo.CreatedByUser, historyInfo.CreatedDate, historyInfo.ProjectID, historyInfo.Activity, historyInfo.ActivityType, historyInfo.ObjectID), Integer)
        End Function


        ''' <summary>
        ''' Updates a specified History
        ''' </summary>
        ''' <returns></returns>
        Public Shared Sub UpdateHistory(ByVal historyInfo As HistoryInfo)
            DataProvider.Instance().UpdateHistory(historyInfo.ItemID, historyInfo.ModuleID, historyInfo.CreatedByUser, historyInfo.CreatedDate, historyInfo.ProjectID, historyInfo.Activity, historyInfo.ActivityType, historyInfo.ObjectID)
        End Sub


        ''' <summary>
        ''' Deletes a specified History
        ''' </summary>
        ''' <returns></returns>
        Public Shared Sub DeleteHistory(ByVal itemID As Integer)
            DataProvider.Instance().DeleteHistory(itemID)
        End Sub


        ''' <summary>
        ''' Retrieves the details of a specified History
        ''' </summary>
        ''' <returns>HistoryInfo object</returns>
        Public Function GetHistoryInfo(ByVal itemID As Integer) As HistoryInfo
            Return CType(CBO.FillObject(DataProvider.Instance().GetHistoryInfo(itemID), GetType(HistoryInfo)), HistoryInfo)
        End Function


        ''' <summary>
        ''' Retrieves the entire list of Histories
        ''' </summary>
        ''' <returns>ArrayList of HistoryInfo objects</returns>
        Public Function GetHistoryList(ByVal projectID As Integer) As ArrayList
            Return CBO.FillCollection(DataProvider.Instance().GetHistoryList(projectID), GetType(HistoryInfo))
        End Function

        ''' <summary>
        ''' Retrieves a specific Activity entry
        ''' </summary>
        ''' <returns>HistoryInfo object</returns>
        Public Function GetHistoryActivity(ByVal projectID As Integer, ByVal activity As String) As HistoryInfo
            Return CType(CBO.FillObject(DataProvider.Instance().GetHistoryActivity(projectID, activity), GetType(HistoryInfo)), HistoryInfo)
        End Function

        ''' <summary>
        ''' Retrieves a specific Activity entry
        ''' </summary>
        ''' <returns>HistoryInfo object</returns>
        Public Function GetHistoryActivityByType(ByVal projectID As Integer, ByVal activityType As String, ByVal objectID As Integer) As HistoryInfo
            Return CType(CBO.FillObject(DataProvider.Instance().GetHistoryActivityByType(projectID, activityType, objectID), GetType(HistoryInfo)), HistoryInfo)
        End Function

        ''' <summary>
        ''' Retrieves the entire list of Histories
        ''' </summary>
        ''' <returns>ArrayList of HistoryInfo objects</returns>
        Public Function GetHistoryListByType(ByVal projectID As Integer, ByVal activityType As String, ByVal objectID As Integer) As ArrayList
            Return CBO.FillCollection(DataProvider.Instance().GetHistoryListByType(projectID, activityType, objectID), GetType(HistoryInfo))
        End Function

#End Region

#Region "Optional Interfaces"

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' ExportModule implements the IPortable ExportModule Interface
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="ModuleID">The Id of the module to be exported</param>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Function ExportModule(ByVal ModuleID As Integer) As String Implements Entities.Modules.IPortable.ExportModule
            Dim strXML As New StringBuilder

            Dim arrList As ArrayList = Me.GetHistoryList(ModuleID)
            If arrList.Count <> 0 Then
                strXML.Append("<histories>")
                For Each objItem As HistoryInfo In arrList
                    strXML.Append("<history>")
                    strXML.Append("<itemid>XMLEncode(objItem.ItemID.ToString())</itemid>")
                    strXML.Append("<moduleid>XMLEncode(objItem.ModuleID.ToString())</moduleid>")
                    strXML.Append("<createdbyuser>XMLEncode(objItem.CreatedByUser.ToString())</createdbyuser>")
                    strXML.Append("<createddate>XMLEncode(objItem.CreatedDate.ToString())</createddate>")
                    strXML.Append("<projectid>XMLEncode(objItem.ProjectID.ToString())</projectid>")
                    strXML.Append("<activity>XMLEncode(objItem.Activity.ToString())</activity>")
                    strXML.Append("<activityType>XMLEncode(objItem.ActivityType.ToString())</activityType>")
                    strXML.Append("<objectID>XMLEncode(objItem.ObjectID.ToString())</objectID>")
                    strXML.Append("</history>")
                Next
                strXML.Append("</histories>")
            End If

            Return strXML.ToString()

        End Function

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' ImportModule implements the IPortable ImportModule Interface
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <param name="ModuleID">The Id of the module to be imported</param>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Sub ImportModule(ByVal ModuleID As Integer, ByVal Content As String, ByVal Version As String, ByVal UserId As Integer) Implements Entities.Modules.IPortable.ImportModule
            Dim xmlList As XmlNode = GetContent(Content, "histories")
            For Each xmlItem As XmlNode In xmlList.SelectNodes("history")
                Dim objItem As New HistoryInfo
                objItem.ItemID = CType(Null.GetNull(xmlItem.Item("itemid").InnerText, Null.NullInteger), Integer)
                objItem.ModuleID = CType(Null.GetNull(xmlItem.Item("moduleid").InnerText, Null.NullInteger), Integer)
                objItem.CreatedByUser = CType(Null.GetNull(xmlItem.Item("createdbyuser").InnerText, Null.NullInteger), Integer)
                objItem.CreatedDate = CType(Null.GetNull(xmlItem.Item("createddate").InnerText, Null.NullDate), DateTime)
                objItem.ProjectID = CType(Null.GetNull(xmlItem.Item("projectid").InnerText, Null.NullInteger), Integer)
                objItem.Activity = CType(Null.GetNull(xmlItem.Item("activity").InnerText, Null.NullString), String)
                objItem.ActivityType = CType(Null.GetNull(xmlItem.Item("activityType").InnerText, Null.NullString), String)
                objItem.ObjectID = CType(Null.GetNull(xmlItem.Item("objectID").InnerText, Null.NullInteger), Integer)

                If objItem.ItemID = Null.NullInteger Then
                    Me.AddHistory(objItem)
                Else
                    Me.UpdateHistory(objItem)
                End If
            Next
        End Sub

#End Region


    End Class

End Namespace
